package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.HospitalQueryParams;
import com.css.fxfzypg.modules.baseData.repository.HospitalRepository;
import com.css.fxfzypg.modules.baseData.entity.HospitalEntity;
import com.css.fxfzypg.modules.baseData.service.HospitalService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-19 10:13
 */
@Service
public class HospitalServiceImpl implements HospitalService {


    @Resource
    private HospitalRepository hospitalRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HospitalEntity saveHospital(HospitalEntity hospitalEntity) {
        hospitalEntity.setDhIsdelete(YNEnum.Y.toString());
        hospitalEntity.setDhCreatetime(PlatformDateUtils.getCurrentTimestamp());
        return hospitalRepository.saveHospital(hospitalEntity);
    }

    @Override
    public HospitalEntity findHospitalById(Integer dataHospitalId) {
        return hospitalRepository.findHospitalById(dataHospitalId);
    }

    @Override
    public JSONObject getAllHospitalByParams(HospitalQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(hospitalRepository.getAllHospitalByParams(queryParams));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHospital(List<Integer> ids) {
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        hospitalRepository.deleteHospital(ids,currentTimestamp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response) throws IOException {
              List<HospitalEntity> list = hospitalRepository.exportExcel();
        FileUtil.exportExcel(list, "医院数据", "sheet", HospitalEntity.class, "HospitalExcel", response);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<HospitalEntity> hospitalEntities = FileUtil.importExcel(file, HospitalEntity.class);
        for (HospitalEntity hospitalEntity : hospitalEntities) {
            hospitalEntity.setDhCreatetime(PlatformDateUtils.getCurrentTimestamp());
            hospitalEntity.setDhIsdelete(YNEnum.Y.toString());
        }
        hospitalRepository.importExcel(hospitalEntities);
    }
}
